package com.systop.dxjj.modules.teacher.service.impl;

import com.systop.dxjj.common.pojo.Teacher;
import com.systop.dxjj.common.pojo.TeacherUser;
import com.systop.dxjj.modules.teacher.mapper.TeacherMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("teacherUserDetailsService")
public class TeacherUserDetailsService implements UserDetailsService {

    @Autowired
    TeacherMapper teacherMapper;

    //加载用户

    public UserDetails loadUserByUsername(String teachphone) throws UsernameNotFoundException {
        //1.查询用户信息
        //调用teacherMapper方法，根据用户名查询数据库
        Teacher teachers = teacherMapper.selectTeachByTeachphone(teachphone);
        //2.判断,没有用户就抛异常
        if (teachers == null) {
            throw new RuntimeException("用户名不存在！");
        }
        //3.查询对应的权限信息

        //把数据封装成UserDetails返回
        return new TeacherUser(teachers);
    }
  }
